﻿using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace Teigha.Core
{
	public class OdCharMapper : IDisposable
	{
		[EditorBrowsable(EditorBrowsableState.Never)]
		public OdCharMapper(IntPtr cPtr, bool cMemoryOwn)
		{
			this.swigCMemOwn = cMemoryOwn;
			this.swigCPtr = new HandleRef(this, cPtr);
		}

		[EditorBrowsable(EditorBrowsableState.Never)]
		public static HandleRef getCPtr(OdCharMapper obj)
		{
			if (obj != null)
			{
				return obj.swigCPtr;
			}
			return new HandleRef(null, IntPtr.Zero);
		}

		~OdCharMapper()
		{
			this.Dispose();
		}

		public virtual void Dispose()
		{
			if (this.swigCPtr.Handle != IntPtr.Zero)
			{
				if (this.swigCMemOwn)
				{
					lock (this.locker)
					{
						if (this.swigCPtr.Handle != IntPtr.Zero && this.swigCMemOwn)
						{
							this.swigCMemOwn = false;
							GlobalsPINVOKE.delete_OdCharMapper(this.swigCPtr);
						}
					}
					this.swigCMemOwn = false;
				}
				this.swigCPtr = new HandleRef(null, IntPtr.Zero);
			}
			GC.SuppressFinalize(this);
		}

		public static OdResult initialize(string filename)
		{
			OdResult result = (OdResult)GlobalsPINVOKE.OdCharMapper_initialize(filename);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public static OdResult unicodeToCodepage(char sourceChar, OdCodePageId codepageId, char codepageChar, bool bTryToUseSystemCP)
		{
			OdResult result = (OdResult)GlobalsPINVOKE.OdCharMapper_unicodeToCodepage__SWIG_0(sourceChar, (int)codepageId, codepageChar, bTryToUseSystemCP);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public static OdResult unicodeToCodepage(char sourceChar, OdCodePageId codepageId, char codepageChar)
		{
			OdResult result = (OdResult)GlobalsPINVOKE.OdCharMapper_unicodeToCodepage__SWIG_1(sourceChar, (int)codepageId, codepageChar);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public static OdResult unicodeToCodepage2(char sourceChar, OdCodePageId codepageId, char codepageChar)
		{
			OdResult result = (OdResult)GlobalsPINVOKE.OdCharMapper_unicodeToCodepage2(sourceChar, (int)codepageId, codepageChar);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public static OdResult codepageToUnicode(char sourceChar, OdCodePageId codepageId, char unicodeChar)
		{
			OdResult result = (OdResult)GlobalsPINVOKE.OdCharMapper_codepageToUnicode(sourceChar, (int)codepageId, unicodeChar);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public static bool isLeadByte(byte testByte, OdCodePageId codepageId)
		{
			bool result = GlobalsPINVOKE.OdCharMapper_isLeadByte(testByte, (int)codepageId);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public static OdResult codepageDescToId(string description, OdCodePageId codepageId)
		{
			OdResult result = (OdResult)GlobalsPINVOKE.OdCharMapper_codepageDescToId(description, codepageId);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public static OdResult codepageIdToDesc(OdCodePageId codepageId, ref string description)
		{
			IntPtr intPtr = Marshal.StringToCoTaskMemUni(description);
			IntPtr value = intPtr;
			OdResult result;
			try
			{
				OdResult odResult = (OdResult)GlobalsPINVOKE.OdCharMapper_codepageIdToDesc((int)codepageId, ref intPtr);
				if (GlobalsPINVOKE.SWIGPendingException.Pending)
				{
					throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
				}
				result = odResult;
			}
			finally
			{
				if (intPtr != value)
				{
					description = Marshal.PtrToStringUni(intPtr);
				}
			}
			return result;
		}

		public static uint numValidCodepages()
		{
			uint result = GlobalsPINVOKE.OdCharMapper_numValidCodepages();
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public static OdCodePageId ansiCpToAcadCp(uint ansiCodePage)
		{
			OdCodePageId result = (OdCodePageId)GlobalsPINVOKE.OdCharMapper_ansiCpToAcadCp(ansiCodePage);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public static uint acadCpToAnsiCp(OdCodePageId acadCodePageId)
		{
			uint result = GlobalsPINVOKE.OdCharMapper_acadCpToAnsiCp((int)acadCodePageId);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public static OdCodePageId getCodepageByCharset(ushort ansiCharacterSet)
		{
			OdCodePageId result = (OdCodePageId)GlobalsPINVOKE.OdCharMapper_getCodepageByCharset(ansiCharacterSet);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public static bool isConversionSupported(OdCodePageId codepageId)
		{
			bool result = GlobalsPINVOKE.OdCharMapper_isConversionSupported((int)codepageId);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public static void wideCharToMultiByte(OdCodePageId codePage, string srcBuf, int srcSize, SWIGTYPE_p_OdArrayT_char_OdMemoryAllocatorT_char_t_t dstBuf)
		{
			GlobalsPINVOKE.OdCharMapper_wideCharToMultiByte((int)codePage, srcBuf, srcSize, SWIGTYPE_p_OdArrayT_char_OdMemoryAllocatorT_char_t_t.getCPtr(dstBuf));
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
		}

		public static void multiByteToWideChar(OdCodePageId codePage, string srcBuf, int srcSize, OdCharArray dstBuf)
		{
			GlobalsPINVOKE.OdCharMapper_multiByteToWideChar((int)codePage, srcBuf, srcSize, OdCharArray.getCPtr(dstBuf));
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
		}

		public static void utf8ToUnicode(string srcBuf, int srcSize, OdCharArray dstBuf)
		{
			GlobalsPINVOKE.OdCharMapper_utf8ToUnicode(srcBuf, srcSize, OdCharArray.getCPtr(dstBuf));
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
		}

		public static void unicodeToUtf8(string srcBuf, int srcSize, SWIGTYPE_p_OdArrayT_char_OdMemoryAllocatorT_char_t_t dstBuf)
		{
			GlobalsPINVOKE.OdCharMapper_unicodeToUtf8(srcBuf, srcSize, SWIGTYPE_p_OdArrayT_char_OdMemoryAllocatorT_char_t_t.getCPtr(dstBuf));
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
		}

		public static OdResult addBigFontWithIndex(string bigFont, int cpIndex)
		{
			OdResult result = (OdResult)GlobalsPINVOKE.OdCharMapper_addBigFontWithIndex(bigFont, cpIndex);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public static OdResult addBigFontWithCodepage(string bigFont, OdCodePageId codePageId)
		{
			OdResult result = (OdResult)GlobalsPINVOKE.OdCharMapper_addBigFontWithCodepage(bigFont, (int)codePageId);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public static OdResult addBigFonts(OdStreamBuf io)
		{
			OdResult result = (OdResult)GlobalsPINVOKE.OdCharMapper_addBigFonts(OdStreamBuf.getCPtr(io));
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public static OdCodePageId getCpByBigFont(string bigFont)
		{
			OdCodePageId result = (OdCodePageId)GlobalsPINVOKE.OdCharMapper_getCpByBigFont(bigFont);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public static int getCpIndexByBigFont(string bigFont)
		{
			int result = GlobalsPINVOKE.OdCharMapper_getCpIndexByBigFont(bigFont);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public static double getCheckSumAnsi(string str)
		{
			double result = GlobalsPINVOKE.OdCharMapper_getCheckSumAnsi(str);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public static double getCheckSumUnicode(string str)
		{
			double result = GlobalsPINVOKE.OdCharMapper_getCheckSumUnicode(str);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		private object locker = new object();

		private HandleRef swigCPtr;

		protected bool swigCMemOwn;
	}
}
